﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using WucCloud.Application;
using WucCloud.Entity;
using WucCloud.Utility;

namespace WucCloud.WebApi.Areas.SystemManage.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Area("SystemManage")]
    [Authorize(Policy = "AdminOrSystem")]
    public class RoleInfoController : ControllerBase
    {
        /// <summary>
        /// 角色服务类
        /// </summary>
        private readonly RoleInfoService _service;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service">角色服务类</param>
        public RoleInfoController(RoleInfoService service)
        {
            _service = service;
        }

        #region 获取数据
        /// <summary>
        /// 通过Id查询
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<UnifyResult>> FindById([Required] string keyId)
        {
            try
            {
                //通过Id查询数据
                var entity = await _service.FindById(keyId);
                //判断是否获取到数据
                if (entity == null)
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.NoneData,
                        Message = ResultCode.NoneData.Description(),
                        Data = null,
                        TotalCount = 0
                    };
                }
                //返回获取到的数据
                else
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description(),
                        Data = entity,
                        TotalCount = 1
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }

        /// <summary>
        /// 自定义条件查询
        /// </summary>
        /// <param name='keyvalue'>查询条件(编码/名称)</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<UnifyResult>> GetList(string keyvalue)
        {
            try
            {
                var entity = await _service.GetList(keyvalue);

                //判断是否获取到数据
                if (entity.Count == 0)
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.NoneData,
                        Message = ResultCode.NoneData.Description(),
                        Data = null,
                        TotalCount = 0
                    };
                }
                //返回获取到的数据
                else
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description(),
                        Data = entity,
                        TotalCount = entity.Count
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }

        /// <summary>
        /// 自定义条件分页查询
        /// </summary>
        /// <param name='keyvalue'>查询条件（编码/名称）</param>
        /// <param name='pageIndex'>页数</param>
        /// <param name='pageSize'>行数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<UnifyResult>> QueryPagedList(string keyvalue, [Required] int pageIndex, [Required] int pageSize)
        {
            //分页组件类
            PageModel page = new PageModel
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };

            try
            {
                //获取分页数据
                var entity = await _service.QueryPagedList(keyvalue, page);

                //判断是否获取到数据
                if (entity.Count == 0)
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.NoneData,
                        Message = ResultCode.NoneData.Description(),
                        Data = null,
                        TotalCount = 0
                    };
                }
                //返回获取到的数据
                else
                {
                    return new UnifyResult<dynamic>
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description(),
                        Data = entity,
                        TotalCount = entity.Count
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存数据（新增/编辑）
        /// </summary>
        /// <param name='keyId'>主键Id</param>
        /// <param name='entity'>实体信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<UnifyResult>> SaveChange(string keyId, [FromForm] RoleInfoEntity entity)
        {
            try
            {
                bool result = await _service.SaveChange(keyId, entity);
                //操作失败
                if (!result)
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Failed,
                        Message = ResultCode.Failed.Description()
                    };
                }
                //操作成功
                else
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description()
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ActionResult<UnifyResult>> Deleteable([Required] string keyId)
        {
            try
            {
                bool result = await _service.Deleteable(keyId);
                //操作失败
                if (!result)
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Failed,
                        Message = ResultCode.Failed.Description()
                    };
                }
                //操作成功
                else
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description()
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }

        /// <summary>
        /// 假删（逻辑删除）
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ActionResult<UnifyResult>> SoftDeleteable([Required] string keyId)
        {
            try
            {
                bool result = await _service.SoftDeleteable(keyId);
                //操作失败
                if (!result)
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Failed,
                        Message = ResultCode.Failed.Description()
                    };
                }
                //操作成功
                else
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description()
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="keyIds">主键Id集合，以英文逗号分割</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ActionResult<UnifyResult>> BatchDeleteable([Required] string[] keyIds)
        {
            try
            {
                bool result = await _service.BatchDeleteable(keyIds);
                //操作失败
                if (!result)
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Failed,
                        Message = ResultCode.Failed.Description()
                    };
                }
                //操作成功
                else
                {
                    return new UnifyResult()
                    {
                        StatusCode = ResultCode.Success,
                        Message = ResultCode.Success.Description()
                    };
                }
            }
            catch (Exception ex)
            {
                return new UnifyResult()
                {
                    StatusCode = ResultCode.Error,
                    Message = ex.Message.ToString()
                };
            }
        }
        #endregion
    }
}
